<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 李邦琦 <1536549251@qq.com>
// +----------------------------------------------------------------------

namespace app\crm\model;
use think\Db;
use think\Model;
use think\Cache;
use think\Loader;

class Illness extends Model
{
    /**
     * 缓存信息
     */
    protected function illnesscache ()
    {
        $result = Db::name('vip_illness_type')->where("enabled='Y'")->select();
        foreach ($result as $v) {
            $data[$v['id']] = $v;
        }
        Cache::set('illnessSelect', $data);
    }


    /**
     * 获取病情类型列表
     * @return array <日报列表信息>
     */
    public function illnessData()
    {
        $page = $_REQUEST["page"]; // 页码
        $limit = $_REQUEST["limit"]; // 每页显示条数
        $count = Db::name('vip_illness_type')->count();
        // 查询状态为1的用户数据 并且每页显示10条数据
        $illnessDataList = Db::name('vip_illness_type')->limit(($page-1)*$limit,$limit)->order('id ASC')->select();
        foreach ($illnessDataList as &$val) {
            $val['enabled'] = $val['enabled'] == 'Y'? '显示': '不显示';
        }

        $rowsList = count($illnessDataList) ? $illnessDataList : false;
        $arrData["code"] = 0;
        $arrData["msg"] = '';
        $arrData["count"] = $count;
        $arrData["data"] = $rowsList;
        return $arrData;
    }


    /**
     * 删除病情类型
     * @param  string $id 病情类型id
     * @return json       删除状态
     */
    public function delIllness ($id)
    {
        $result = Db::name('vip_illness_type')->delete($id);
        if ($result) {
            $this->illnesscache();
            echo json_encode(array('success'=>true,'msg'=>'删除成功！'));
        } else {
            echo json_encode(array('msg'=>'删除失败！'));
        }
    }


    /**
     * 新增病情类型
     * @param array  $data  数据
     * @return json         新增状态
     */
    public function insertIllness ($data)
    {
        // print_r($data);exit;
        $result = Db::name('vip_illness_type')->insert($data);
        if ($result) {
            $this->illnesscache();
            echo json_encode(array('success'=>true,'msg'=>'添加成功！'));
        } else {
            echo json_encode(array('msg'=>'添加失败！'));
        }
    }


    /**
     * 编辑病情类型
     * @param array  $data  数据
     * @return json         新增状态
     */
    public function updateIllness ($data)
    {
        $result = Db::name('vip_illness_type')->update($data);
        if ($result) {
            $this->illnesscache();
            echo json_encode(array('success'=>true,'msg'=>'编辑成功！'));
        } else {
            echo json_encode(array('msg'=>'编辑失败！'));
        }
    }


    /**
     * 获取单条病情类型信息
     * @param string $id    数据id
     * @return array        选项信息
     */
    public function getIllness ($id)
    {
        $result = Db::name('vip_illness_type')->where("id=$id")->find();
        return $result;
    }


}